package com.bitters.springbootjpa;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

/**
 * description: 消失的數字
 * author: thomashe
 * date: 2022/4/1
 */
public class FindNumberDisappared {

    public static void main(String[] args) {
        //int[] nums = {4, 3, 2, 7, 8, 2, 3, 1};
        int[] nums2 = {1, 2, 4, 5,1};
        System.out.println(disapparedNumber2(nums2));
    }

    public static Vector<Integer> disapparedNumber(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        Vector<Integer> vector = new Vector<Integer>();
        for (int num : nums) {
            set.add(num);
        }
        System.out.println(set.toString());
        for (int i = 1; i < nums.length; i++) {
            System.out.println(set.contains(nums[i]));
            if (!set.contains(i)) {
                vector.add(i);
            }
        }
        return vector;
    }

    public static Vector<Integer> disapparedNumber2(int[] nums) {
        Vector<Integer> vector = new Vector<Integer>();
        for (int i = 0; i < nums.length; i++) {
            int num = Math.abs(nums[i]);
            int index = num - 1;
            if (nums[index] > 0) {
                nums[index] = -nums[index];
                System.out.println("索引"+index+""+ Arrays.toString(nums));
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                vector.add(i+1);
            }
        }
        return vector;
    }

}
